package com.shchuangzhi.smartguard.core.command;

import com.shchuangzhi.smartguard.core.Consts;
import com.shchuangzhi.smartguard.core.DBHelper;
import com.shchuangzhi.smartguard.core.Notify;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by j_hu on 5/23/17.
 */

public class DecisionCommand extends Command {

    private Command activeCommand;
    private List<Command> commandList;

    public DecisionCommand() {
        super(Consts.COMMAND_DECISION);
        this.commandList = new ArrayList<>();
        this.commandList.add(new UploadCommand());
        this.commandList.add(new SyncMetaCommand());
    }

    @Override
    public void setDBHelper(DBHelper dbHelper) {
        for(Command cmd : commandList) {
            cmd.setDBHelper(dbHelper);
        }
    }

    @Override
    public void setNotifier(Notify notifier) {
        for(Command cmd : commandList) {
            cmd.setNotifier(notifier);
        }
    }

    @Override
    protected String getType() {
        return activeCommand.getType();
    }

    @Override
    protected CommandHandler getHandler(String commandName) {
        return activeCommand.getHandler(commandName);
    }

    @Override
    protected OutputStream fill(OutputStream output) {
        return activeCommand.fill(output);
    }

    @Override
    public CommandResult runPost() {
        CommandResult result = new CommandResult();
        for(Command cmd : commandList) {
            if (cmd.isAvailable()) {
                activeCommand = cmd;
                break;
            }
        }
        if (activeCommand != null) {
            result = activeCommand.runPost();
        }
        return result;
    }
}
